package com.qf.spring002.dao.impl;


import com.qf.spring002.dao.UserDao;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class UserDaoImpl implements UserDao {
    private String name ;

    private Integer age;

    private String filepath;

    private BufferedReader bufferedReader;

    public UserDaoImpl() {
        System.out.println("1.New: 调用 UserDaoImpl 的构造函数");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("2.DI: 调用 UserDaoImpl 的setAge 方法,参数为："+name);
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        System.out.println("2.DI: 调用 UserDaoImpl 的setAge 方法,参数为："+age);
        this.age = age;
    }

    public String getFilepath() {
        return filepath;
    }

    public void setFilepath(String filepath) {
        System.out.println("2.DI: 调用 UserDaoImpl 的setAge 方法,参数为："+filepath);
        this.filepath = filepath;
    }

    public BufferedReader getBufferedReader() {
        return bufferedReader;
    }

    public void setBufferedReader(BufferedReader bufferedReader) {

        this.bufferedReader = bufferedReader;
    }

    // 自定义的初始化方法
    public void init()  {
        System.out.println("3.初始化: 调用 UserDaoImpl 的init方法");
        // 把filepath对应的文件转换成输入流
        try {
            bufferedReader = new BufferedReader(new FileReader(filepath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    // 销毁对象的方法
    public void destroy(){
        System.out.println("4.销毁对象: 调用 UserDaoImpl 的destroy方法");

        try {
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    @Override
    public String getUserByid(Integer id) {
        return "UserDaoImpl get user";
    }
}
